---
- hosts: application
  tasks:
    - name: Install MySQL client and other required packages
      apt: name={{ item }} update_cache=yes cache_valid_time=3600 state=present
      with_items:
          - mysql-client

    - name: Prepare app
      copy:
        src: app/    # note the '/' <-- !!!
        dest: /app/

    - name: Installing python requirements for app
      pip:
        requirements: requirements.txt
        chdir: /app

    - name: Prepare env for launch
      template:
        src: app/env.j2
        dest: /app/.env
        owner: root
        mode: '0600' #- rw—— - -
    - name: Killing previously started pythons (if any)
      shell: "ps aux | grep -i app.py | awk {'print $2'} | xargs kill -9"
      ignore_errors: yes
    - name: Start the app
      shell: "cd /app; nohup python3 app.py </dev/null >/var/log/app 2>&1 &"

